package com.yanqu.road.dao.impl.activity.recallplayer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.recallplayer.data.RecallPlayerFloorCellData;
import com.yanqu.road.entity.activity.recallplayer.data.UserRecallSelfData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserRecallSelfDataDaoImpl extends TempDataObjectDao<UserRecallSelfData> {

    public static String getUesrRecallSelfDataSql = "select * from `t_u_recall_player_self_data` where `userId`=?";
    public UserRecallSelfData getUserRecallSelfData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUesrRecallSelfDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserRecallSelfDataSql = "update `t_u_recall_player_self_data` set recallCode=?,hasFirstShared=?,remainingLotteryTimes=?,hasDrawTimeThisFloor=?,floor=?,cellDataList=?,totalDrawTime=? where userId=?";
    @Override
    protected boolean updateData(UserRecallSelfData userRecallSelfData) {
        userRecallSelfData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, userRecallSelfData.getRecallCode()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userRecallSelfData.getHasFirstShared()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getRemainingLotteryTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getHasDrawTimeThisFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallSelfData.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getTotalDrawTime()));
        para.put(i++, new DbParameter(Types.BIGINT, userRecallSelfData.getUserId()));
        if (getDb().executeNoneQuery(updateUserRecallSelfDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserRecallSelfDataSql = "insert into `t_u_recall_player_self_data`(userId,recallCode,hasFirstShared,remainingLotteryTimes,hasDrawTimeThisFloor,floor,cellDataList,totalDrawTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserRecallSelfData userRecallSelfData) {
        userRecallSelfData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRecallSelfData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRecallSelfData.getRecallCode()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userRecallSelfData.getHasFirstShared()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getRemainingLotteryTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getHasDrawTimeThisFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getFloor()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userRecallSelfData.getCellDataList())));
        para.put(i++, new DbParameter(Types.INTEGER, userRecallSelfData.getTotalDrawTime()));
        if (getDb().executeNoneQuery(insertUserRecallSelfDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String getUserRecallCodeMapSql = "SELECT `userId`,`recallCode` FROM `t_u_recall_player_self_data`";
    public Map<Long,String> getUserRecallCodeMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long,String> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserRecallCodeMapSql);
            rs = call.executeQuery();
            while (rs.next()) {
                resultMap.put(rs.getLong("userId"),rs.getString("recallCode"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }


    @Override
    protected UserRecallSelfData parseData(ResultSet rs) throws SQLException {
        UserRecallSelfData userRecallSelfData = new UserRecallSelfData();
        userRecallSelfData.setUserId(rs.getLong("userId"));
        userRecallSelfData.setRecallCode(rs.getString("recallCode"));
        userRecallSelfData.setHasFirstShared(rs.getBoolean("hasFirstShared"));
        userRecallSelfData.setRemainingLotteryTimes(rs.getInt("remainingLotteryTimes"));
        userRecallSelfData.setHasDrawTimeThisFloor(rs.getInt("hasDrawTimeThisFloor"));
        userRecallSelfData.setFloor(rs.getInt("floor"));
        userRecallSelfData.setCellDataList(JSON.parseObject(rs.getString("cellDataList"), new TypeReference<List<RecallPlayerFloorCellData>>() {}));
        userRecallSelfData.setTotalDrawTime(rs.getInt("totalDrawTime"));
        return userRecallSelfData;
    }
}
